package com.chen.authority.controller;

import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.dto.PostDto;
import com.chen.authority.model.qo.PostQo;
import com.chen.authority.model.vo.PostVo;
import com.chen.authority.service.PostService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "岗位管理 PostController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/post")
public class PostController {

    private final PostService postService;

    @ApiOperation("新增岗位")
    @SaCheckPermission("system:post:add")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response add(@Valid @RequestBody PostDto dto) {
        return postService.addOrUpdatePost(null, dto);
    }

    @ApiOperation("删除岗位")
    @SaCheckPermission("system:post:delete")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Response delete(@PathVariable Integer[] ids) {
        return postService.deletePostByIds(ids);
    }

    @ApiOperation("更新岗位")
    @SaCheckPermission("system:post:update")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public Response update(@PathVariable Integer id, @Valid @RequestBody PostDto dto) {
        return postService.addOrUpdatePost(id, dto);
    }

    @ApiOperation("查询指定岗位")
    @SaCheckPermission("system:post:update")
    @GetMapping("/{id}")
    public SingleResponse<PostVo> detail(@PathVariable Integer id) {
        return SingleResponse.success(postService.getById(id).toVo(PostVo.class));
    }

    @ApiOperation("查询岗位列表")
    @SaCheckPermission("system:post:list")
    @GetMapping("/list")
    public PageResponse<PostVo> list(PageQo pageQo, PostQo postQo) {
        return PageResponse.success(postService.pageQueryPostList(pageQo, postQo));
    }

    @ApiOperation("导出岗位列表")
    @SaCheckPermission("system:post:export")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostQo qo) {
        ExcelUtil.exportExcel(response, "岗位表", PostVo.class, postService.exportPostList(WrapperUtil.buildQueryWrapper(qo)));
    }

}